
#include "Core.h"
#include "BiCE.h"

#include <stdio.h>
#include <math.h>

#include <vector>
#include <utility>
using namespace std;

BiCE::BiCE(int _height,int _width,const unsigned char *data)
{

	height = _height;
	width = _width;

	// count how many patch to be calculate
	vector< pair<int,int> > lefttop_point;
	int patch_x_dim = 0;
	int patch_y_dim = 0;
	patch_x_dim =(int) ceil( (float) (width - PATCH_SIZE) / (PATCH_SHIFT) ) + 1;
	patch_y_dim =(int) ceil( (float) (height - PATCH_SIZE) / (PATCH_SHIFT) ) + 1;
	
	//printf("%d,%d\n",height,width);
	//printf("%d,%d\n",patch_y_dim,patch_x_dim);
	
	// count the left-top position for each patch
	int i,j,k;
	for(i=0;i<patch_y_dim;i++) {
		int y_position;
		y_position = (i != patch_y_dim -1) ? i*(PATCH_SHIFT) : height - PATCH_SIZE;
		for(j=0;j<patch_x_dim;j++) {
			int x_position;
			x_position = (j != patch_x_dim-1) ? j*(PATCH_SHIFT) : width - PATCH_SIZE ;
			lefttop_point.push_back(make_pair( y_position,x_position));
			//printf("%d,%d\n",y_position,x_position);
		}
	}
	
	unsigned char patch[PATCH_SIZE][PATCH_SIZE] = {0};
	
	// assign value to patch build descriptor and record postion_sketch_pairs
	for(k=0;k<lefttop_point.size();k++) {
		//printf("( %d,%d )\n",lefttop_point[i].first, lefttop_point[i].second);
		int y,x;
		y = lefttop_point[k].first;
		x = lefttop_point[k].second;
		for(i=0;i<PATCH_SIZE;i++) {
			for(j=0;j<PATCH_SIZE;j++) {
				patch[i][j] = data[pos(y+i,x+j)];
				//printf("%d ", (int)patch[i][j]);
			}
			//puts("");
		}

		// build descriptor for this patch
		Descriptor des(patch);
		
		// if(des.isgood())  only add this sketch if this descriptor is good
		if( des.noBitOn == false ) {
			for(i=0;i<NUM_SKETCH_PER_DES;i++) {
				//cout << Position(y,x) << des.sketches[i] << endl;
				postion_sketch_pairs.push_back( make_pair( Position(y,x), des.sketches[i]) );
			}
		}
	}
}
